package com.rs.game.player.controlers.quests.lostcity;

import com.rs.Settings;
import com.rs.game.Entity;
import com.rs.game.ForceTalk;
import com.rs.game.WorldObject;
import com.rs.game.WorldTile;
import com.rs.game.npc.others.TreeSpirit;
import com.rs.game.player.QuestManager.Quests;
import com.rs.game.player.actions.Woodcutting;
import com.rs.game.player.actions.Woodcutting.TreeDefinitions;
import com.rs.game.player.controlers.Controler;

public class LostCityDungeon extends Controler {
	
	private TreeSpirit target;
	
	@Override
	public boolean canAttack(Entity target) {
		if (target instanceof TreeSpirit && target != this.target) {
			player.getPackets().sendGameMessage("This isn't your target.");
			return false;
		}
		return true;
	}
	
	@Override
	public boolean sendDeath() {
		leave(false);
		return true;
	}
	
	@Override
	public void magicTeleported(int type) {
		leave(false);
	}
	
	@Override
	public boolean logout() {
		leave(true);
		return false;
	}
	
	public boolean login() {
		return false;
	}
	
	@Override
	public void forceClose() {
		leave(true);
	}
	
	@Override
	public boolean processObjectClick1(WorldObject object) {
		if (object.getId() == 1292) {
			if (target != null) {
				player.getPackets().sendGameMessage("Nothing interesting happens.");
				return false;
			}
			if (!player.spawnedBossLC && player.getQuestManager().getQuestStage(Quests.LOST_CITY) >= 1 && player.getQuestManager().getQuestStage(Quests.LOST_CITY) <= 3) {
				sendTarget(655, player);
			}
			if (player.getQuestManager().getQuestStage(Quests.LOST_CITY) == 4) {
				player.getActionManager().setAction(new Woodcutting(object, TreeDefinitions.DRAMEN));
			}
		}
		return true;
	}
	
	public void leave(boolean logout) {
		if (target != null) {
			player.spawnedBossLC = false;
			target.finish();
		}
		if (!logout) {
			removeControler();
		}
	}
	
	public void sendTarget(int id, WorldTile tile) {
		if (target != null) {
			target.disappear();
		}
		target = new TreeSpirit(id, tile, this);
		target.setTarget(player);
		target.setNextForceTalk(new ForceTalk("You must defeat me before touching the tree!"));
		player.getHintIconsManager().addHintIcon(target, 1, -1, false);
		player.spawnedBossLC = true;
	}
	
	public void targetDied() {
		player.getHintIconsManager().removeUnsavedHintIcon();
		player.getQuestManager().setQuestStageAndRefresh(Quests.LOST_CITY, 4);
		target = null;
	}
	
	public LostCityDungeon() {

	}

	@Override
	public void start() {
		if (Settings.DEBUG) {
			player.getPackets().sendGameMessage("LostCityDungeon Controler starter");
		}
	}
}